#include "myado.h"
//*#include <iostream.h>
//*#include <stdio.h>
const char * MyAdo::connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
MyAdo::MyAdo()
{
	hr = ::CoInitialize(NULL);
	if(SUCCEEDED(hr))
	{
		CreateDataBase();
		CreateTable();
		
		::CoUninitialize();
	}
}
MyAdo::MyAdo(const char * path)
{
	hr = ::CoInitialize(NULL);
	if(NULL == path)
	{
		if(SUCCEEDED(hr))
		{
			CreateDataBase();
			CreateTable();
		
			::CoUninitialize();
		}
	}
	else
	{
		if(SUCCEEDED(hr))
		{
			CreateDataBase(path);
			CreateTable(path);
		
			::CoUninitialize();
		}
	}
}
MyAdo::MyAdo(const MyAdo &ado)
{
	
}
MyAdo::~MyAdo()
{
	delete []strCnn;
	if(NULL != strCnn)
	{
		strCnn = NULL;
	}
}
void MyAdo::CreateDataBase(const char * path)
{
	if(NULL == path)
		this->filePath = "e:\\test1.mdb";
	else
		this->filePath = path;
	strCnn = new char [strlen(filePath)+strlen(connStr)+1];
	//*cout<<strlen(strCnn)<<endl;
	strCnn = strcpy(strCnn,connStr);
	strCnn = strcat(strCnn,filePath);
	//*cout<<strlen(strCnn)<<endl;
	//*cout<<strCnn<<endl;
	HRESULT hR = S_OK;
	_bstr_t strcnn(strCnn);

	try 
	{
		ADOX::_CatalogPtr	m_pCatalog = NULL;
		hR = m_pCatalog.CreateInstance(__uuidof(ADOX::Catalog));
		if(FAILED(hR ))
		{
			_com_issue_error(hR);
		}
		else
		{
			m_pCatalog->Create(strcnn);
		}
	}
	catch(_com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());

		//*printf("\n\tSource: %s  \n\t Description:  %s  \n ",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
	}
	catch(...)
	{
		//*cout<< "Error occured in incude files....."<<endl;
	}
}
void MyAdo::CreateTable(const char * path)
{
	if(NULL == path)
		this->filePath = "e:\\test1.mdb";
	else
		this->filePath = path;
	HRESULT		hr = S_OK;

	_bstr_t	strSQLCreatetable("CREATE TABLE URL(ID INTEGER,URL TEXT)");
	_bstr_t	strSQLCreatetable2("CREATE TABLE EMAIL(ID INTEGER,EMAIL TEXT)");
	m_Conn = NULL;
	m_Rset = NULL;
	m_Comm = NULL;


	
	try
	{	
		//CREATE CONNECTION OBJECT
		TESTHR(m_Conn.CreateInstance(__uuidof(Connection)));
		m_Conn->Open((_bstr_t)(strCnn),"","",adModeUnknown);		

		//CREATE COMMAND OBJECT
		TESTHR(m_Comm.CreateInstance(__uuidof(Command)));
		m_Comm->ActiveConnection = m_Conn;
		m_Comm->CommandText = strSQLCreatetable;
		ExecuteCommand(m_Comm,m_Rset);
		m_Comm->CommandText = strSQLCreatetable2;
		ExecuteCommand(m_Comm,m_Rset);
	}	
	catch(_com_error e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());

		//*printf("\n\tSource: %s  \n\t Description:  %s  \n ",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);

	}

}
void MyAdo::ExecuteCommand(_CommandPtr pCmdTemp, _RecordsetPtr pRstTemp)
{
	try
    {
        // CommandText property already set before function was called.
        pCmdTemp->Execute(NULL, NULL, adCmdText);

        // Retrieve the current data by requerying the recordset.
        pRstTemp->Requery(adCmdUnknown);
    }

    catch(_com_error &e)
    {
        // Notify user of any errors that result from
        // executing the query.
        // Pass a connection pointer accessed from the Recordset.
        _bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());

		//*printf("\n\tSource: %s  \n\t Description:  %s  \n ",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
    }	
}

void MyAdo::addData()
{

}
void MyAdo::deleteData()
{

}
void MyAdo::readData()
{

}
void MyAdo::amendData()
{

}